function renderInput(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }
  if (val.properties.allowClear == '1') {
    val.extendAttrs['allow-clear'] = true
  } else {
    val.extendAttrs['allow-clear'] = false
  }
  if (val.properties.colon == '1') {
    val.attrs['colon'] = true
  } else {
    val.attrs['colon'] = false
  }
  if (val.properties.displayTitle == '1') {
    val.attrs['display-title'] = true
  } else {
    val.attrs['display-title'] = false
  }

  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  renderValidator(val)
}
function renderInputNumber(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }
  if (val.properties.allowClear == '1') {
    val.extendAttrs['allow-clear'] = true
  }
  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  const array = []
  if (val.properties.required == '1') {
    array.push({ required: true, message: val.properties.label + '不能为空' })
  }
  if (val.properties.min) {
    array.push({ min: val.properties.min, message: '最短为' + val.properties.min + '位' })
  }
  if (val.properties.max) {
    array.push({ max: val.properties.max, message: '最长为' + val.properties.max + '位' })
  }
  if (val.properties.pattern) {
    array.push({ pattern: val.properties.pattern, message: val.properties.label + '格式不正确' })
  }
  if (array.length > 0) {
    val.attrs.rules = array
  }
  if (val.properties.minValue) {
    val.extendAttrs['min'] = val.properties.minValue
  }
  if (val.properties.maxValue) {
    val.extendAttrs['max'] = val.properties.maxValue
  }
}
function renderSlider(val) {
  renderInput(val)
  val.extendAttrs['min'] = val.properties.min
  val.extendAttrs['max'] = val.properties.max
  val.extendAttrs['step'] = val.properties.step
  delete val.extendAttrs.dot
  delete val.extendAttrs.range
  delete val.extendAttrs.marks
  delete val.extendAttrs.orientation
  if (val.properties.dot == '1') {
    val.extendAttrs.step = null
    val.extendAttrs['dot'] = true
  }
  if (val.properties.range == '1') {
    val.extendAttrs['range'] = true
  }
  if (val.properties.orientation == '2') {
    val.extendAttrs['vertical'] = true
  }
  if (val.properties.marks) {
    const data = {}
    const array = val.properties.marks
    array.forEach((item) => {
      data[item.value] = item.label
    })
    val.extendAttrs['marks'] = data
  }
}

function renderColorPicker(val) {
  renderInput(val)
  const data = {}
  const array = val.properties.dictData
  array.forEach((item) => {
    data[item.value] = item.label
  })
  val.extendAttrs['data'] = data
  if (val.properties.checkBoxColor) {
    val.extendAttrs['checkBoxColor'] = val.properties.checkBoxColor
  } else {
    delete val.extendAttrs.checkBoxColor
  }
  if (val.properties.enablePicker != '1') {
    val.extendAttrs['enable-picker'] = false
  } else {
    delete val.extendAttrs['enable-picker']
  }
}
function renderTag(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }
  if (val.properties.colon == '1') {
    val.attrs['colon'] = true
  } else {
    val.attrs['colon'] = false
  }
  if (val.properties.displayTitle == '1') {
    val.attrs['display-title'] = true
  } else {
    val.attrs['display-title'] = false
  }

  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  const array = []
  if (val.properties.required == '1') {
    array.push({ whitespace: true, required: true, message: val.properties.label + '不能为空' })
  }
  if (array.length > 0) {
    val.attrs.rules = array
  }
  delete val.extendAttrs.max
  delete val.extendAttrs.color
  if (val.properties.max) {
    val.extendAttrs['max'] = val.properties.max
  }
  if (val.properties.color) {
    val.extendAttrs['color'] = val.properties.color
  }
}
function renderDatetime(val) {
  renderInput(val)
  val.extendAttrs['type'] = val.properties.dateType
  if (val.properties.dateFormat) {
    val.extendAttrs['value-format'] = val.properties.dateFormat
  }
  if (val.properties.showTime == '1') {
    val.extendAttrs['show-time'] = true
  }
}
function renderImg(val) {
  renderInput(val)
  if (val.properties.maxValue) {
    val.extendAttrs['max'] = val.properties.maxValue
  }
}
function renderFile(val) {
  renderInput(val)
  if (val.properties.maxValue) {
    val.extendAttrs['max'] = val.properties.maxValue
  }
}
function renderCard(val) {
  val.attrs['title'] = val.properties.label
  if (val.properties.bordered == '1') {
    val.attrs['bordered'] = true
  }
  val.extendAttrs['height'] = val.properties.height
}
function renderTab(val) {
  val.attrs['type'] = val.properties.type
  val.attrs['tabPosition'] = val.properties.tabPosition
  val.attrs['size'] = val.properties.size
  val.extendAttrs['tabs'] = val.properties.tabs.split(',')
  val.extendAttrs['height'] = val.properties.height
  val.children = []
}
function renderTextArea(val) {
  renderInput(val)
  val.extendAttrs['rowNum'] = val.properties.rowNum
}
function renderDivider(val) {
  val.attrs['orientation'] = val.properties.orientation
  if (val.properties.dashed == '1') {
    val.attrs['dashed'] = true
  }
}
function renderSwitch(val) {
  renderInput(val)
  if (val.properties.trueValue) {
    val.extendAttrs['check-value'] = val.properties.trueValue
  }
  if (val.properties.falseValue) {
    val.extendAttrs['un-check-value'] = val.properties.falseValue
  }
}
function renderIconSelect(val) {
  renderInput(val)
}
function renderRate(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }
  if (val.properties.allowClear == '1') {
    val.extendAttrs['allow-clear'] = true
  } else {
    val.extendAttrs['allow-clear'] = false
  }
  if (val.properties.colon == '1') {
    val.attrs['colon'] = true
  } else {
    val.attrs['colon'] = false
  }
  if (val.properties.displayTitle == '1') {
    val.attrs['display-title'] = true
  } else {
    val.attrs['display-title'] = false
  }

  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  const array = []
  if (val.properties.required == '1') {
    array.push({ required: true, message: val.properties.label + '不能为空' })
  }
  if (array.length > 0) {
    val.attrs.rules = array
  }
  if (val.properties.allowHalf == '1') {
    val.extendAttrs['allow-half'] = true
  } else {
    val.extendAttrs['allow-half'] = false
  }
  val.extendAttrs['icon'] = val.properties.icon
  val.extendAttrs['count'] = parseInt(val.properties.starCount)
  if (val.properties.tooltips) {
    val.extendAttrs['tooltips'] = val.properties.tooltips.split(',')
  } else {
    val.extendAttrs['tooltips'] = null
  }
}
function renderSelect(val) {
  renderInput(val)
  if (val.properties.search == '1') {
    val.extendAttrs['show-search'] = true
  }
  if (val.properties.mode == '2') {
    val.extendAttrs['mode'] = 'multiple'
  }
  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
    val.extendAttrs['url'] = null
    val.extendAttrs['data'] = null
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['code'] = null
    val.extendAttrs['data'] = null
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
  }
  if (val.properties.dictType == '3') {
    const data = []
    const array = val.properties.dictData
    array.forEach((item) => {
      data.push({ label: item.label, value: item.value })
    })
    val.extendAttrs['data'] = data
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = null
  }
}

function renderTransfer(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }
  if (val.properties.colon == '1') {
    val.attrs['colon'] = true
  } else {
    val.attrs['colon'] = false
  }
  if (val.properties.displayTitle == '1') {
    val.attrs['display-title'] = true
  } else {
    val.attrs['display-title'] = false
  }

  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  renderValidator(val)
  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
    val.extendAttrs['url'] = null
    val.extendAttrs['data'] = null
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['code'] = null
    val.extendAttrs['data'] = null
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
  }
  if (val.properties.dictType == '3') {
    const data = []
    const array = val.properties.dictData
    array.forEach((item) => {
      data.push({ title: item.label, key: item.value })
    })
    val.extendAttrs['data'] = data
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = null
  }
}
function renderSelectTree(val) {
  renderInput(val)
  delete val.extendAttrs['expand']
  if (val.properties.expand == '0') {
    val.extendAttrs['expand-all'] = false
  }
  if (val.properties.search == '1') {
    val.extendAttrs['show-search'] = true
  }
  if (val.properties.checkChildren == '0') {
    val.extendAttrs['check-children'] = false
  }
  if (val.properties.mode == '2') {
    val.extendAttrs['multiple'] = true
  }
  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
    val.extendAttrs['url'] = null
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
    val.extendAttrs['key-field'] = val.properties.dictIdKey
    val.extendAttrs['parent-key-field'] = val.properties.dictParentIdKey
  }
}

function renderTree(val) {
  val.attrs = {
    label: val.properties.label,
    size: val.properties.size,
    name: val.properties.key
  }

  if (val.properties.colon == '1') {
    val.attrs['colon'] = true
  } else {
    val.attrs['colon'] = false
  }
  if (val.properties.displayTitle == '1') {
    val.attrs['display-title'] = true
  } else {
    val.attrs['display-title'] = false
  }

  if (val.properties.tip) {
    val.attrs.tip = val.properties.tip
  }
  if (val.properties.placeholder) {
    val.extendAttrs.placeholder = val.properties.placeholder
  }
  if (val.properties.size == 'custom') {
    val.attrs.options = {
      labelWidth: val.properties.labelWidth,
      wrapperWidth: val.properties.wrapperWidth,
      offsetWidth: val.properties.offsetWidth,
      total: val.properties.totalWidth
    }
  }
  renderValidator(val)
  val.extendAttrs['value-type'] = 'String'
  delete val.extendAttrs['width']
  delete val.extendAttrs['height']
  delete val.extendAttrs['expand']
  delete val.extendAttrs['check-children']
  if (val.properties.expand == '0') {
    val.extendAttrs['expand-all'] = false
  }
  if (val.properties.width) {
    val.extendAttrs['width'] = val.properties.width
  }
  if (val.properties.height) {
    val.extendAttrs['height'] = val.properties.height
  }
  if (val.properties.checkChildren == '0') {
    val.extendAttrs['check-children'] = false
  }

  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
    val.extendAttrs['url'] = null
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
    val.extendAttrs['key-field'] = val.properties.dictIdKey
    val.extendAttrs['parent-key-field'] = val.properties.dictParentIdKey
  }
}
function renderCascader(val) {
  renderInput(val)
  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
    val.extendAttrs['url'] = null
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
    val.extendAttrs['key-field'] = val.properties.dictIdKey
    val.extendAttrs['parent-key-field'] = val.properties.dictParentIdKey
  }
}
function renderRadio(val) {
  renderInput(val)

  if (val.properties.dictType == '1') {
    val.extendAttrs['code'] = val.properties.dictCode
  }
  if (val.properties.dictType == '2') {
    val.extendAttrs['url'] = val.properties.dictUrl
    val.extendAttrs['label-field'] = val.properties.dictLabelKey
    val.extendAttrs['value-field'] = val.properties.dictValueKey
  }
  if (val.properties.dictType == '3') {
    const data = []
    const array = val.properties.dictData
    array.forEach((item) => {
      data.push({ label: item.label, value: item.value })
    })
    val.extendAttrs['data'] = data
    val.extendAttrs['code'] = null
    val.extendAttrs['url'] = null
  }
}
function renderValidator(val) {
  const array = []
  if (val.properties.required == '1') {
    array.push({ whitespace: true, required: true, message: val.properties.label + '不能为空' })
  }
  if (val.properties.min) {
    array.push({ min: val.properties.min, message: '最短为' + val.properties.min + '位' })
  }
  if (val.properties.max) {
    array.push({ max: val.properties.max, message: '最长为' + val.properties.max + '位' })
  }
  if (val.properties.pattern) {
    array.push({ pattern: val.properties.pattern, message: val.properties.label + '格式不正确' })
  }
  if (array.length > 0) {
    val.attrs.rules = array
  }
}
function renderComponentAttrs(obj) {
  switch (obj.type) {
    case 'input':
      return renderInput(obj)
    case 'textarea':
      return renderTextArea(obj)
    case 'select':
      return renderSelect(obj)
    case 'radio':
      return renderRadio(obj)
    case 'checkbox':
      return renderRadio(obj)
    case 'inputNumber':
      return renderInputNumber(obj)
    case 'switch':
      return renderSwitch(obj)
    case 'selectTree':
      return renderSelectTree(obj)
    case 'datetime':
      return renderDatetime(obj)
    case 'img':
      return renderImg(obj)
    case 'file':
      return renderFile(obj)
    case 'divider':
      return renderDivider(obj)
    case 'card':
      return renderCard(obj)
    case 'tab':
      return renderTab(obj)
    case 'province':
      return renderInput(obj)
    case 'cascader':
      return renderCascader(obj)
    case 'iconSelect':
      return renderIconSelect(obj)
    case 'rate':
      return renderRate(obj)
    case 'slider':
      return renderSlider(obj)
    case 'tag':
      return renderTag(obj)
    case 'transfer':
      return renderTransfer(obj)
    case 'tree':
      return renderTree(obj)
    case 'colorPicker':
      return renderColorPicker(obj)
  }
}
export default renderComponentAttrs
